package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.CartQueryBean;
import com.wwp.tbshop.presenter.CartPresenter;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.PriceFormater;
import com.wwp.tbshop.view.fragment.diyview.CartFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by windows on 2017/6/15.
 */

public class CartARAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private CartPresenter mPresenter;
    //    private boolean mIsChecked;
    private CartFragment mCartFragment;

    public CartARAdapter(CartPresenter presenter) {
        mPresenter = presenter;
    }

    private List<CartQueryBean.CartBean> mCartBeenList = new ArrayList<>();
    private List<CartQueryBean.CartBean> mCartUpdateBeanList = new ArrayList<>();
    private List<CartQueryBean.CartBean> mCartDeleteBeen = new ArrayList<>();

    private int mPosition;

    public CartARAdapter(Context context, CartPresenter presenter, CartFragment cartFragment) {
        mContext = context;
        mPresenter = presenter;
        mCartFragment = cartFragment;
    }

    public void setCartBeanInfo(List<CartQueryBean.CartBean> cartBeen) {

        this.mCartBeenList.clear();
        this.mCartBeenList.addAll(cartBeen);

        notifyDataSetChanged();
    }

    public void setCartUpdateBeanInfo(List<CartQueryBean.CartBean> cartUpdateBean) {
        this.mCartUpdateBeanList.clear();
        this.mCartUpdateBeanList.addAll(cartUpdateBean);
        notifyDataSetChanged();
    }

    public void setCartDeleteBeanInfo(List<CartQueryBean.CartBean> cartDeleteBeen) {
        this.mCartDeleteBeen = cartDeleteBeen;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_shop_goodsinfo, parent, false);
        ShopItemHolder viewHolder = new ShopItemHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ShopItemHolder shopItemHolder = (ShopItemHolder) holder;
        if (mCartBeenList.size() != 0) {
            shopItemHolder.setDatas(mCartBeenList.get(position), position);
        }

    }

    @Override
    public int getItemCount() {

        if (mCartBeenList.size() > 0) {
            return mCartBeenList.size();
        }
        return 0;

    }

    /**
     * 判断是否全选
     * @return
     */
    public boolean isAllChecked() {

        for (CartQueryBean.CartBean cartBean : mCartBeenList) {
            if (!cartBean.isChecked()) {
                return false;
            }
        }
        return true;

    }

    /**
     *计算总价
     * @return
     */
    public float countTotalPrice(){
        float totalPrice = 0;
        for (CartQueryBean.CartBean cartBean : mCartBeenList) {
            if (cartBean.isChecked()){
                totalPrice += cartBean.getProductCount() * cartBean.getProduct().getPrice();
            }
        }

        return totalPrice;
    }

    public CartQueryBean.CartBean mCurentUpdate;

    public void onUpdateResult() {
        mCartFragment.updatCartInfo();
    }
    class ShopItemHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.shopcart_item_no_goods)
        ImageView mShopcartItemNoGoods;
        @Bind(R.id.shopcart_item_check)
        CheckBox mShopcartItemCheck;
        @Bind(R.id.shopcart_item_image)
        ImageView mShopcartItemImage;
        @Bind(R.id.shopcart_item_title)
        TextView mShopcartItemTitle;
        @Bind(R.id.shopcart_item_color_size)
        TextView mShopcartItemColorSize;
        @Bind(R.id.shopcart_item_reduce)
        ImageButton mShopcartItemReduce;
        @Bind(R.id.shopcart_item_edit)
        TextView mShopcartItemEdit;
        @Bind(R.id.shopcart_item_add)
        ImageButton mShopcartItemAdd;
        @Bind(R.id.shopcart_item_price)
        TextView mShopcartItemPrice;
        @Bind(R.id.shopcart_item_old_price)
        TextView mShopcartItemOldPrice;
        @Bind(R.id.shopcart_item_more)
        ImageView mShopcartItemMore;
        @Bind(R.id.btnDelete)
        Button mBtnDelete;
        private CartQueryBean.CartBean mCartBean;
        public int mPosition;
        private int mCount;



        @OnClick({R.id.shopcart_item_add, R.id.shopcart_item_reduce})
        public void onAddOrMinusClick(View view) {

            switch (view.getId()) {
                case R.id.shopcart_item_add:
                    doAddOperation();
                    break;
                case R.id.shopcart_item_reduce:
                    doMinusOperation();
                    break;
            }

        }

        //点击数量的增加
        private void doAddOperation() {

            mCurentUpdate = mCartBean;
            int count = mCartBean.getProductCount();
            count++;
            mPresenter.updateCartInfo(mCartBean.getUserId() + "", mCartBean.getProductId() + "", count + "", mCartBean.getPropertyId() + "");
            mCartBean.setProductCount(count);
            notifyDataSetChanged();
            Log.d("count", "doAddOperation: "+count);

        }

        //点击数量的减少
        private void doMinusOperation() {
            mCurentUpdate = mCartBean;
            int count = mCartBean.getProductCount();
            Log.d("count", "doMinusOperation: 服务器的数据:" + count);

            count--;

            //当count等于0的时候,数据库的删除及UI页面的删除
            if (count == 0) {
                mPresenter.deleteCartInfo(mCartBean.getUserId() + "", mCartBean.getProductId() + "");
                mCartBeenList.remove(mCartBean);
                notifyDataSetChanged();
            }

            mPresenter.updateCartInfo(mCartBean.getUserId() + "", mCartBean.getProductId() + "", count + "", mCartBean.getPropertyId() + "");

            //显示数量
            mCartBean.setProductCount(count);

            Log.d("count", "doMinusOperation: 显示数据" + count);

            notifyDataSetChanged();

            //更新购物车信息
            mCartFragment.updatCartInfo();

        }



        public ShopItemHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }

        public void setDatas(final CartQueryBean.CartBean bean, int position) {

            this.mPosition = position;
            this.mCartBean = bean;
            mShopcartItemTitle.setText(bean.getProduct().getName());
            int count = bean.getProductCount();

            //数字的显示
            if (count > 0) {
                mShopcartItemEdit.setText(count + "");
            }

            //颜色及尺寸的显示
            mShopcartItemColorSize.setText("颜色:" + bean.getProperty().getK() + ";尺码:" + bean.getProperty().getV());

            //商品图片的显示
            Picasso.with(mContext).load(Constant.HOST + bean.getProduct().getPic()).into(mShopcartItemImage);

            //更多及原价的显示
            mShopcartItemOldPrice.setVisibility(View.GONE);
            mShopcartItemMore.setVisibility(View.GONE);

            mShopcartItemPrice.setText(PriceFormater.format(count * bean.getProduct().getPrice()));

            mShopcartItemCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    bean.setChecked(isChecked);
                    mCartFragment.selectAll();
                    mCartFragment.updatCartInfo();

                    //TODO:清空显示空页面


                }
            });

            if (bean.isChecked()) {
                mShopcartItemCheck.setChecked(true);
            } else {
                mShopcartItemCheck.setChecked(false);
            }

            //删除功能
            mBtnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.deleteCartInfo(mCartBean.getUserId() + "", mCartBean.getProductId() + "");
                    mCartBeenList.remove(bean);
                    mCartFragment.updatCartInfo();
                    notifyDataSetChanged();
                }
            });

        }
    }
}